package com.cmcc.groupcontacts.firewall.activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.res.ColorStateList;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cmcc.groupcontacts.MainApp;
import com.cmcc.groupcontacts.firewall.activity.strategy.BaseTitleStrategy;
import com.cmcc.groupcontacts.util.ActivityHelper;
import com.cmcc.groupcontacts.util.DisplayMetricsConverter;
import com.yinhai.activity.R;

/**
 * 
 * Activity抽象基类
 * 
 * @author
 * 
 */
public abstract class BaseActivity extends Activity {
	private static final String LOG_TAG = "BaseActivity";

	protected ActivityHelper helper = new ActivityHelper(this);
	protected String activityName; // 当前Activity的名称
	protected BaseTitleStrategy titleStrategy; // 标题栏显示策略

	public Drawable getDrawable(int drawableId) {
		return getResources().getDrawable(drawableId);
	}

	public int getColor(int colorId) {
		return getResources().getColor(colorId);
	}

	public ColorStateList getColorStateList(int colorStateListId) {
		return getResources().getColorStateList(colorStateListId);
	}

	public BaseTitleStrategy getTitleStrategy() {
		return titleStrategy;
	}

	protected DisplayMetricsConverter getDmConverter() {
		return MainApp.getDmConverter();
	}

	protected SharedPreferences getPreference() {
		return MainApp.getPreference();
	}

	public LayoutInflater getInflater() {
		return helper.getInflater();
	}

	public Display getDisplay() {
		return helper.getDisplay();
	}

	/**
	 * 获取标题栏布局控件
	 * 
	 * @return
	 */
	public RelativeLayout getTitleLayout() {
		return titleStrategy.getTitleLayout();
	}

	/**
	 * 获取标题栏文本控件
	 * 
	 * @return
	 */
	public TextView getTitleTextView() {
		return titleStrategy.getTitleTextView();
	}

	/**
	 * 获取标题栏左边布局
	 * 
	 * @return
	 */
	public LinearLayout getLeftLayout() {
		return titleStrategy.getLeftLayout();
	}

	/**
	 * 获取标题栏中间布局
	 * 
	 * @return
	 */
	public LinearLayout getCenterLayout() {
		return titleStrategy.getCenterLayout();
	}

	/**
	 * 获取标题栏右边布局
	 * 
	 * @return
	 */
	public LinearLayout getRightLayout() {
		return titleStrategy.getRightLayout();
	}

	/**
	 * 创建标题栏默认样式图片按钮
	 * 
	 * @return
	 */
	public ImageButton createImageButton() {
		return titleStrategy.createImageButton();
	}

	/**
	 * 创建标题栏默认样式按钮
	 * 
	 * @return
	 */
	public Button createButton() {
		return titleStrategy.createButton();
	}

	/**
	 * 创建标题栏默认返回图片按钮
	 * 
	 * @return
	 */
	public ImageButton createBackButton() {
		ImageButton btnBack = createImageButton();
		btnBack.setBackgroundDrawable(null);
		btnBack.setImageResource(R.drawable.btn_back1);
		btnBack.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		return btnBack;
	}

	/**
	 * 创建标题栏默认新增图片按钮
	 * 
	 * @return
	 */
	public ImageButton createAddButton() {
		ImageButton btnAdd = createImageButton();
		// int padding =
		// getResources().getDimensionPixelSize(R.dimen.common_spacing_large);
		// btnAdd.setPadding(padding, padding, padding, padding);
		// btnAdd.setLayoutParams(new
		// ViewGroup.LayoutParams(getDmConverter().DipToPixel(20),
		// getDmConverter().DipToPixel(20)));
		// LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)
		// btnAdd.getLayoutParams();
		// params.width = params.height = getDmConverter().DipToPixel(30);
		// params.rightMargin = getDmConverter().DipToPixel(10);
		// btnAdd.setScaleType(ScaleType.FIT_XY);
		btnAdd.setImageResource(R.drawable.ic_add);
		return btnAdd;
	}

	// ===== events =====

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		activityName = this.getClass().getName();

		setTitleStrategy();
	}

	@Override
	public void setContentView(int layoutResID) {
		titleStrategy.setWindowFeature(layoutResID);

		setTitleButtons();
		titleStrategy.setTitleText();
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onResume() {
		super.onResume();
		try {
			// MobclickAgent.onResume(this);
		} catch (Exception e) {
		}

		// 设置全局背景
		getWindow().setBackgroundDrawable(getCommonBackgroundDrawable());
		titleStrategy.setTitleSkin();
	}

	protected Drawable getCommonBackgroundDrawable() {
		Drawable drawable = getDrawable(R.drawable.default_backgroud);
		if (drawable instanceof BitmapDrawable) {
			((BitmapDrawable) drawable).setTileModeXY(TileMode.REPEAT,
					TileMode.REPEAT);
		}
		return drawable;
	}

	@Override
	protected void onPause() {
		super.onPause();
		try {
			// MobclickAgent.onPause(this);
		} catch (Exception e) {
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	// ===== override methods =====

	/**
	 * 设置标题栏显示策略<br />
	 */
	public abstract void setTitleStrategy();

	/**
	 * 设置标题栏按钮
	 */
	public void setTitleButtons() {
	};
}
